//
//  TTDefaultTableViewCell.swift
//  TTSwiftHub
//
//  Created by QDSG on 2020/6/15.
//  Copyright © 2020 tTao. All rights reserved.
//

import UIKit

class TTDefaultTableViewCell: TTTableViewCell {
    
    override func makeUI() {
        super.makeUI()
        
        themeService.rx
            .bind({ $0.text }, to: titleLabel.rx.textColor)
            .bind({ $0.textGray }, to: detailLabel.rx.textColor)
            .bind({ $0.text }, to: secondDetailLabel.rx.textColor)
            .bind({ $0.secondary }, to: [leftImageView.rx.tintColor, rightImageView.rx.tintColor])
            .disposed(by: rx.disposeBag)
        
        stackView.addArrangedSubviews([leftImageView, textsStackView, rightImageView])
        stackView.snp.remakeConstraints { (make) in
            let inset = self.inset
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: inset / 2, left: inset, bottom: inset / 2, right: inset))
            make.height.greaterThanOrEqualTo(Configs.BaseDimensions.tableRowHeight)
        }
    }
    
    override func bindViewModel(to viewModel: TTTableViewCellViewModel) {
        super.bindViewModel(to: viewModel)
        
        guard let viewModel = viewModel as? TTDefaultTableViewCellViewModel else { return }
        
        viewModel.title.asDriver().drive(titleLabel.rx.text).disposed(by: rx.disposeBag)
        viewModel.title.asDriver().replaceNilWith("").map { $0.isEmpty }.drive(titleLabel.rx.isHidden).disposed(by: rx.disposeBag)
        
        viewModel.detail.asDriver().drive(detailLabel.rx.text).disposed(by: rx.disposeBag)
        viewModel.detail.asDriver().replaceNilWith("").map { $0.isEmpty }.drive(detailLabel.rx.isHidden).disposed(by: rx.disposeBag)
        
        viewModel.secondDetail.asDriver().drive(secondDetailLabel.rx.text).disposed(by: rx.disposeBag)
        viewModel.secondDetail.asDriver().replaceNilWith("").map { $0.isEmpty }.drive(secondDetailLabel.rx.isHidden).disposed(by: rx.disposeBag)
        
        viewModel.attributedDetail.asDriver().drive(attributedDetailLabel.rx.attributedText).disposed(by: rx.disposeBag)
        viewModel.attributedDetail.asDriver().map { $0 == nil }.drive(attributedDetailLabel.rx.isHidden).disposed(by: rx.disposeBag)
        
        viewModel.badge.asDriver().drive(badgeImageView.rx.image).disposed(by: rx.disposeBag)
        viewModel.badge.map { $0 == nil }.asDriver(onErrorJustReturn: true).drive(badgeImageView.rx.isHidden).disposed(by: rx.disposeBag)
        
        viewModel.badgeColor.asDriver().drive(badgeImageView.rx.tintColor).disposed(by: rx.disposeBag)
        
        viewModel.hidesDisclosure.asDriver().drive(rightImageView.rx.isHidden).disposed(by: rx.disposeBag)
        
        viewModel.image.asDriver().filterNil().drive(leftImageView.rx.image).disposed(by: rx.disposeBag)
        
        viewModel.imageUrl.map { $0?.url }.asDriver(onErrorJustReturn: nil).filterNil()
            .drive(leftImageView.rx.imageURL)
            .disposed(by: rx.disposeBag)
        
        viewModel.imageUrl.asDriver().filterNil().drive(onNext: { [weak self] url in
            self?.leftImageView.hero.id = url
        }).disposed(by: rx.disposeBag)
    }
    
    // MARK: - Lazy Load
    lazy var leftImageView: TTImageView = {
        let view = TTImageView(frame: CGRect())
        view.contentMode = .scaleAspectFit
        view.cornerRadius = 25.0
        view.snp.makeConstraints({ (make) in
            make.size.equalTo(50)
        })
        return view
    }()
    
    lazy var badgeImageView: TTImageView = {
        let view = TTImageView(frame: CGRect())
        view.backgroundColor = .white
        view.cornerRadius = 10
        view.borderColor = .white
        view.borderWidth = 1
        containerView.addSubview(view)
        view.snp.makeConstraints({ (make) in
            make.bottom.left.equalTo(self.leftImageView)
            make.size.equalTo(20)
        })
        return view
    }()
    
    lazy var titleLabel: TTLabel = {
        let label = TTLabel()
        label.font = label.font.withSize(14)
        return label
    }()
    
    lazy var detailLabel: TTLabel = {
        let label = TTLabel()
        label.font = label.font.withSize(12)
        label.setPriority(.defaultLow, for: .vertical)
        return label
    }()
    
    lazy var secondDetailLabel: TTLabel = {
        let label = TTLabel()
        label.font = label.font.bold.withSize(11)
        return label
    }()
    
    lazy var attributedDetailLabel: TTLabel = {
        let label = TTLabel()
        label.font = label.font.bold.withSize(11)
        return label
    }()
    
    lazy var textsStackView: TTStackView = {
        let views: [UIView] = [self.titleLabel, self.detailLabel, self.secondDetailLabel, self.attributedDetailLabel]
        let view = TTStackView(arrangedSubviews: views)
        view.spacing = 2
        return view
    }()
    
    lazy var rightImageView: TTImageView = {
        let view = TTImageView(frame: CGRect())
        view.image = R.image.icon_cell_disclosure()?.template
        view.snp.makeConstraints({ (make) in
            make.width.equalTo(20)
        })
        return view
    }()
}
